/**
 * 
 * author:mengxn
 * date:2016年8月31日3:18:42
 */
var keyCompetMaintance = {
	/**
	 * 加载成功回调
	 * 
	 */
	successCallBack: function (res) {
		if (res.status == 1) {
			layer.msg(res.message);
		} else {
			common.hideLoading();
			$("#keyCompet_tbody").empty();
			//console.log(res);
			if (res.data.length > 0) {
				for (var i = 0; i < res.data.length; i++) {
					var compatible_product_name = res.data[i].compatible_product_name;
					var dtcreate = res.data[i].dtcreate;
					var sealmark = res.data[i].sealmark;
					var create_name = res.data[i].create_name;
					var update_name = res.data[i].update_name;
					var update_date = res.data[i].update_date;
					var pk_compatible = res.data[i].pk_compatible;
					var pk_product = res.data[i].pk_product;
					if (res.data[i].product_name == undefined) {
						var product_name = '';
					} else {
						var product_name = res.data[i].product_name;
					}
					if (res.data[i].create_name == undefined) {
						var create_name = '';
					} else {
						var create_name = res.data[i].create_name;
					}
					if (res.data[i].update_name == undefined) {
						var update_name = '';
					} else {
						var update_name = res.data[i].update_name;
					}
					if (res.data[i].update_date == undefined) {
						var update_date = '';
					} else {
						var update_date = res.data[i].update_date;
					}
					if (sealmark == "停用") {
						var mark = "btn-disable onfale";
					} else {
						var mark = "btn-unlock";
					}
					//拼接竞品列表html
					var loadHtml = '<tr id="tr"><td >'
						+ compatible_product_name
						+ '</td><td> <span style="text-align:left; display:block">'
						+ product_name
						+ '</span></td><td >'
						+ sealmark
						+ '</td><td>'
						+ create_name
						+ '</td><td>'
						+ dtcreate
						+ '</td><td >'
						+ update_name
						+ '</td><td>'
						+ update_date
						+ '</td><td><div class="operation"><a class="btn-editor" pk_product ="'
						+ pk_product
						+ '" pk_compatible ="'
						+ pk_compatible
						+ '" name="'
						+ compatible_product_name
						+ '" ></a><a  class="btn-delete" id="delete" name="'
						+ compatible_product_name
						+ '"   pk_compatible="' + pk_compatible
						+ '"></a><a   id="start" name="'
						+ compatible_product_name + '" pk_product ="'
						+ pk_product + '" pk_compatible="'
						+ pk_compatible + '" sealmark="' + sealmark
						+ '" class=" start keyCompet_start ' + mark
						+ '"></a></div></td></tr>';
					$("#keyCompet_tbody").append(loadHtml);

				}
			} else {
				loadHtml = '<tr><td colspan="8">暂无竞品数据</td></tr>';
				$("#keyCompet_tbody").append(loadHtml);
			}
			//产生分页结构
			common.getPageDivide(res.count, "pageDivideContainer", keyCompetMaintance.keyCompet_search);
			//点击事件
			keyCompetMaintance.bindEvent();
		}
	},
	/**
	 * 页面初始化方法
	 * search为空查全部
	 */
	loadDataList: function (start) {
		if (start == "start") {

		} else {
			window.sessionStorage.removeItem('keyCompetOrderKey');
			window.sessionStorage.removeItem('keyCompetDataType');
			var str = "../admin/images/uap.png";
			$("#keyCompet_compatible a").html('<img src="' + str + '"/>');
			$("#keyCompet_dtcreate a").html('<img src=""/>');
			$("#keyCompet_update a").html('<img src=""/>');
		}
		keyCompetMaintance.keyCompet_search();

	},

	/**
	 * 删除方法
	 * pk为整个页面的id
	 */
	keyCompet_delete: function (pk) {
		var userid = window.localStorage.userid;
		var data = {
			"pk_user": userid,
			"pk": pk
		};
		var url = "/compatible/delete";
		common.interfaceAjaxFunction(url, data, this.delsuccess, true);
		common.showLoading();
	},
	/**
	 * 删除成功回调
	 */
	delsuccess: function (res) {
		common.hideLoading();
		//console.log(res);
		if (res.status == 0) {
			layer.msg('删除成功');
			keyCompetMaintance.loadDataList("start");
		} else {
			layer.msg('删除失败，请稍候重试');
		}
	},
	/**
	 * 启用、警用方法
	 * pk 为这个列表id
	 * sealmark为启用状态 Y N
	 * pk_product
	 */
	keyCompet_start: function (pk, sealmark, pk_product) {
		var userid = window.localStorage.userid;
		var data = {
			"pk_user": userid,
			"pk": pk,
			"enable": sealmark,
			"product": pk_product
		};
		var url = "/compatible/update";
		common.interfaceAjaxFunction(url, data, this.startsuccess, true);
		common.showLoading();
	},
//	/**
//	 * 启用、禁用成功回调
//	 */
//	startsuccess: function (res) {
//		keyCompetMaintance.loadDataList("start");
//		var key_sealmark = window.sessionStorage.sealmark;
//		if (res.status == 0) {
//			if (key_sealmark == "Y") {
//				layer.msg("启用成功");
//			} else {
//				layer.msg("停用成功");
//			}
//			common.hideLoading();
//		} else {
//			layer.msg(res.message);
//		}
//	},
	/**
	 * 搜索方法
	 * orderkey 按哪种方式排序
	 * orderrule ASC为升序 DESC为降序
	 */
	keyCompet_search: function () {
		var orderkey = window.sessionStorage.keyCompetOrderKey;
        var orderrule = window.sessionStorage.keyCompetDataType;
		var param = {
			"pk_user": window.localStorage.userid,
			"pageSize": common.pageSize,
			"pageNum": common.currentPage,
			"name": $("#compettkeyInput").val(),
			"orderkey": orderkey ? orderkey : "compatible_product_name",
			"orderrule": orderrule ? orderrule : "ASC"
		};

		//请求参数
		var url = "/compatible/pageQuery";
		common.interfaceAjaxFunction(url, param, keyCompetMaintance.successCallBack);
		common.showLoading();
	},
	/**
	 * 页面绑定事件
	 */
	bindEvent: function () {
		/**
		 * 搜索
		 */
		$("#key_serch").unbind().on("click", function () {
			common.currentPage = 1;//每次搜索从第一页加载；
			keyCompetMaintance.keyCompet_search();
		});
		/**
		 * 编辑
		 */
		$(".btn-editor").unbind().on("click", function () {
			var key_productId = $(this).attr("pk_product");
			var pk_product = $(this).attr("pk_compatible");
			//console.log("壳牌产品pk", key_productId);
			//console.log("当前编辑记录PK", pk_product);
			//sessionStorage传值 跳转页面取
			window.sessionStorage.key_productId = key_productId;
			window.sessionStorage.pk_product = pk_product;
			window.sessionStorage.editor = 1;
			var editor = $(this).parent().parent().parent()[0].children[0];
			var editor_product = $(this).parent().parent().parent()[0].children[1];
			window.sessionStorage.editor_product = $(editor_product).text();
			window.sessionStorage.editor_text = $(editor).text();
			/**
			 * 弹编辑窗口
			 */
			layer.ready(function () {
				//增加编辑
				layer.open({
					type: 2,
					//skin: 'layui-layer-lan',
					title: '增加/编辑',
					fix: false,
					shadeClose: true,
					maxmin: true,
					area: ['1000px', '500px'],
					content: 'html/keyCompetMaintanceAdd.html',
				});
			});

		});
		/**
		 * 删除
		 */
		$(".btn-delete").unbind().on("click", function () {
			var pk = $(this).attr("pk_compatible");
			//console.log(pk);
			var del_text = $(this).parent().parent().parent()[0].children[0];
			var del_name = $(del_text).text();
			//console.log(del_name);
			layer.confirm("您确定删除竞品名称:" + del_name + "？", { area: ['400px', '200px'], title: '删除' }, function () {
				keyCompetMaintance.keyCompet_delete(pk);
			}, function () {

			});
			//keyCompetMaintance.del(pk);

		});
		/**
		 *竞品名称排序
		 */
        $(".keyCompet_order").unbind().on("click", function () {
			var down = "../admin/images/down.png";
			var up = "../admin/images/uap.png";
			$(".keyCompet_order a").html("");
			var type = $(this).attr("data-type");
			if (type) {
				if (type == "ASC") {
					$(this).attr("data-type", "DESC");
					$(this).children('a').html('<img src="' + up + '"/>');

				} else if (type == "DESC") {
					$(this).attr("data-type", "ASC");
					$(this).children('a').html('<img src="' + down + '"/>');

				} else {
					$(this).attr("data-type", "DESC");
					$(this).children('a').html('<img src="' + up + '"/>');
					type = "ASC";
				}
			} else {
				$(this).attr("data-type", "DESC");
				$(this).children('a').html('<img src="' + up + '"/>');
				type = "ASC";
			}
			window.sessionStorage.setItem("keyCompetOrderKey", $(this).attr("order-key"));
			window.sessionStorage.setItem("keyCompetDataType", type);
			common.currentPage = 1;//每次搜索从第一页加载；
			keyCompetMaintance.keyCompet_search();
        });
		/**
		 * 禁用启用 Y N
		 */
		$(".keyCompet_start").unbind().on("click", function () {
			var pk_compatible = $(this).attr("pk_compatible");
			var pk_product = $(this).attr("pk_product");
			if ($(this).attr("sealmark") == "停用") {
				window.sessionStorage.sealmark = "Y";
				var sealmark = "Y";
				keyCompetMaintance.keyCompet_start(pk_compatible, sealmark, pk_product);
			} else if($(this).attr("sealmark") == "启用"){
				window.sessionStorage.sealmark = "N";
				var sealmark = "N";
				keyCompetMaintance.keyCompet_start(pk_compatible, sealmark, pk_product);
			}
		});
		/**
		 * 点击增加
		 */
		$("#keyCompet_add").unbind().on("click", function () {
			//竞品id
			window.sessionStorage.key_productId = '';
			//0为编辑 1为新增
			window.sessionStorage.editor = 0;
			layer.ready(function () {
				//增加编辑
				layer.open({
					type: 2,
					//skin: 'layui-layer-lan',
					title: '增加/编辑',
					fix: false,
					shadeClose: true,
					maxmin: true,
					area: ['1000px', '500px'],
					content: 'html/keyCompetMaintanceAdd.html',
				});
			});
		});
	},
	initialize : function(){
		keyCompetMaintance.loadDataList();
		keyCompetMaintance.bindEvent();//绑定事件
	}
};